package kr.saeildamil.androidstudy.ch19;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class CalcThread extends Thread {
	Handler mMainHandler;
	Handler mBackHandler;

	public CalcThread(Handler mMainHandler) {
		this.mMainHandler = mMainHandler;
	}

	@Override
	public void run() {
		super.run();
		Looper.prepare();
		mBackHandler = new Handler() {
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				Message retMsg = new Message();
				switch (msg.what) {
				case 0:
					try {
						Thread.sleep(200);
					} catch (Exception e) {
					}
					retMsg.what = 0;
					retMsg.arg1 = msg.arg1 * msg.arg1;
					break;
				case 1:
					try {
						Thread.sleep(200);
					} catch (Exception e) {
					}
					retMsg.what = 1;
					retMsg.obj = new Double(Math.sqrt(msg.arg1));
					break;
				}
				mMainHandler.sendMessage(retMsg);
			}
		};
		Looper.loop();
	}
}
